﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using Model;

namespace TanHungLong
{
    public partial class FrmTamUngLuong : DevExpress.XtraEditors.XtraForm
    {
        #region variant
        private FormStatus formStatus = FormStatus.View;
        private bool allowCloseForm = true;
        private TamUngLuongBOCollection tamUngLuongBOCollection = null;

        private string COLUMN_FIRST = "SoTien";
        private string COLUMN_SECOND = "LyDo";
        #endregion variant

        //=====================================
        #region form
        public FrmTamUngLuong()
        {
            InitializeComponent();

            this.Load += new EventHandler(FrmTamUngLuong_Load);
            this.FormClosing += new FormClosingEventHandler(FrmTamUngLuong_FormClosing);

            this.btnRefresh.Click += new EventHandler(btnRefresh_Click);
            this.btnEdit.Click += new EventHandler(btnEdit_Click);
            this.btnSave.Click += new EventHandler(btnSave_Click);
            this.btnCancel.Click += new EventHandler(btnCancel_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);

            this.grdViewNhanVien.RowCellStyle += new RowCellStyleEventHandler(grdViewNhanVien_RowCellStyle);
            this.grdViewTUL.RowCellStyle += new RowCellStyleEventHandler(grdViewTUL_RowCellStyle);

            this.grdViewTUL.ShowingEditor += new CancelEventHandler(grdViewTUL_ShowingEditor);

            this.grdViewNhanVien.CustomDrawGroupRow += new RowObjectCustomDrawEventHandler(grdViewNhanVien_CustomDrawGroupRow);
        }

        private void FrmTamUngLuong_Load(object sender, EventArgs e)
        {
            MyStyle.SetGridStyle(this.grdViewNhanVien, false);
            Utility.InitControl(this.repLookUpPhongBanID);

            RefreshData();
            SetFormStatus(this.formStatus);

            this.grdViewNhanVien.FocusedRowChanged += new FocusedRowChangedEventHandler(grdViewNhanVien_FocusedRowChanged);
        }

        private void FrmTamUngLuong_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.allowCloseForm == false)
                e.Cancel = true;
        }
        #endregion form

        //=====================================
        #region control
        #endregion control

        //=====================================
        #region button
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            Edit();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Cancel();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion button

        //=====================================
        #region grid
        private void grdViewNhanVien_CustomDrawGroupRow(object sender, RowObjectCustomDrawEventArgs e)
        {
            //GridGroupRowInfo info = e.Info as GridGroupRowInfo;
            //int rowHandle = this.grdViewNhanVien.GetDataRowHandleByGroupRowHandle(info.RowHandle);

            //if (info.Column.FieldName == "PhongBanID")
            //{
            //    if (StaticUtil.GetInt(info.EditValue) < int.MaxValue - 1)
            //    {
            //        if (info.EditValue.ToString() != "")
            //        {
            //            tenChiNhanh = this.chiNhanhBOCollection.GetChiNhanhBO((int)info.EditValue).TenChiNhanh;
            //            info.GroupText = tenChiNhanh + " (" + sumText + ")";
            //        }
            //    }
            //    else
            //        if (StaticUtil.GetInt(info.EditValue) == int.MaxValue)
            //            info.GroupText = "Nhân viên mới";
            //        else
            //            info.GroupText = "Chưa có chi nhánh" + " (" + sumText + ")";
            //}
        }

        private void grdViewNhanVien_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            FilterNhanVien();
        }

        private void grdViewTUL_ShowingEditor(object sender, CancelEventArgs e)
        {
            bool del = StaticUtil.GetBool(this.grdViewTUL.GetFocusedRowCellValue("DELE"));
            string fieldName = this.grdViewTUL.FocusedColumn.FieldName;

            if (del && fieldName != "DELE")
                e.Cancel = true;
            else
                if (fieldName == "DELE" && this.grdViewTUL.FocusedRowHandle == GridControl.NewItemRowHandle)
                    e.Cancel = true;
        }

        private void grdViewNhanVien_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
        }

        private void grdViewTUL_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            bool del = StaticUtil.GetBool(this.grdViewTUL.GetRowCellValue(e.RowHandle, "DELE"));

            if (del)
            {
                e.Appearance.Font = MyStyle.DELETE_FONT;
                e.Appearance.ForeColor = MyStyle.DELETE_COLOR;
            }
        }
        #endregion grid

        //=====================================
        #region function
        private void FilterNhanVien()
        {
            Session.TUL_NhanVienID = StaticUtil.GetRealInt(this.grdViewNhanVien.GetFocusedRowCellValue("NhanVienID"));

            if (this.formStatus == FormStatus.Edit)
                this.grdViewTUL.OptionsView.NewItemRowPosition = (Session.TUL_NhanVienID != null ? NewItemRowPosition.Bottom : NewItemRowPosition.None);

            if (Session.TUL_NhanVienID == null)
                this.grdViewTUL.ActiveFilterString = "NhanVienID=" + int.MaxValue;
            else
                this.grdViewTUL.ActiveFilterString = "NhanVienID=" + Session.TUL_NhanVienID.Value.ToString();

            this.grdViewTUL.CheckLoaded();
            SetSttTUL();
        }

        private void RefreshData()
        {
            PhongBanBOList phongBanBOList = PhongBanBOList.GetPhongBanBOList();
            Utility.SetData(this.repLookUpPhongBanID, phongBanBOList, phongBanBOList.Count);

            NhanVienReduceBOCollection nhanVienBOCollection = NhanVienReduceBOCollection.GetNhanVienReduceBOCollection("", "Ten,Ho");
            this.tamUngLuongBOCollection = TamUngLuongBOCollection.GetTamUngLuongBOCollection("", "Ngay", "DESC");

            this.bdSourceNV.DataSource = nhanVienBOCollection;
            this.bdSourceTUL.DataSource = this.tamUngLuongBOCollection.Clone();

            FilterNhanVien();
        }

        private void Edit()
        {
            this.formStatus = FormStatus.Edit;
            SetFormStatus(this.formStatus);
            Session.CloseIndex++;
        }

        private void Save()
        {
            string oldColumn = this.grdViewTUL.FocusedColumn.FieldName;
            string newColumn = (oldColumn == this.COLUMN_FIRST ? this.COLUMN_SECOND : this.COLUMN_FIRST);
            this.grdViewTUL.FocusedColumn = this.grdViewTUL.Columns[newColumn];

            this.bdSourceTUL.EndEdit();

            this.tamUngLuongBOCollection = (TamUngLuongBOCollection)this.bdSourceTUL.DataSource;
            this.tamUngLuongBOCollection = this.tamUngLuongBOCollection.Save();
            this.bdSourceTUL.DataSource = this.tamUngLuongBOCollection.Clone();

            this.grdViewTUL.FocusedColumn = this.grdViewTUL.Columns[oldColumn];

            this.formStatus = FormStatus.View;
            SetFormStatus(this.formStatus);
            Session.CloseIndex--;

            SetSttTUL();
        }

        private void Cancel()
        {
            this.formStatus = FormStatus.View;
            SetFormStatus(this.formStatus);
            Session.CloseIndex--;

            this.bdSourceTUL.DataSource = this.tamUngLuongBOCollection.Clone();

            SetSttTUL();
        }

        private void SetFormStatus(FormStatus formStatus)
        {
            bool edit = (formStatus != FormStatus.View);
            this.allowCloseForm = !edit;

            MyStyle.SetGridStyle(this.grdViewTUL, edit);

            this.btnEdit.Visible = !edit;
            this.btnClose.Visible = !edit;
            this.btnRefresh.Visible = !edit;

            this.btnSave.Visible = edit;
            this.btnCancel.Visible = edit;

            this.colDELE_TUL.Visible = edit;
            foreach (GridColumn col in this.grdViewTUL.Columns)
            {
                if (col.Visible)
                    col.OptionsColumn.AllowEdit = edit;
            }


            this.grdViewTUL.OptionsView.NewItemRowPosition = (edit ? NewItemRowPosition.Bottom : NewItemRowPosition.None);

            if (edit)
                this.grdViewTUL.OptionsView.NewItemRowPosition = (Session.TUL_NhanVienID != null ? NewItemRowPosition.Bottom : NewItemRowPosition.None);
            else
                this.grdViewTUL.OptionsView.NewItemRowPosition = NewItemRowPosition.None;

            this.grdViewTUL.Focus();

            if (edit)
            {
                string focusColumn = this.grdViewTUL.FocusedColumn.FieldName;

                if (string.IsNullOrEmpty(focusColumn))
                    focusColumn = this.COLUMN_FIRST;
                else
                    if (focusColumn == "DELE")
                        focusColumn = this.COLUMN_FIRST;

                if (this.grdViewTUL.RowCount == 1)
                    this.grdViewTUL.FocusedRowHandle = int.MinValue + 1;

                this.grdViewTUL.FocusedColumn = this.grdViewTUL.Columns[focusColumn];
                this.grdViewTUL.ShowEditor();
            }
        }

        private void SetSttTUL()
        {
            if (this.formStatus == FormStatus.Edit)
                this.bdSourceTUL.EndEdit();

            int stt = 0;
            int length = this.grdViewTUL.RowCount;
            int index = 0;
            for (int i = 0; i < length; i++)
            {
                index = this.grdViewTUL.GetVisibleRowHandle(i);
                if (index >= 0)
                    this.grdViewTUL.SetRowCellValue(i, "STT0", ++stt);
            }

            ((TamUngLuongBOCollection)this.bdSourceTUL.DataSource).SttIndex = stt;
        }
        #endregion function
    }
}